package com.tjcd.project.library_base.common.refresh;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.scwang.smart.refresh.layout.api.RefreshComponent;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.simple.SimpleComponent;
import com.tjcd.project.library_base.R;

/**
 * @Author jiangKunKun
 * @CreateDate 2021/7/12
 * @Description 头部刷新视图
 * @SpittingGrooves
 */
public class LoadingHeaderView extends SimpleComponent implements RefreshComponent {
    /**
     * 下拉可以刷新
     */
    public static String REFRESH_HEADER_PULLING = "下拉可以刷新";
    /**
     * 正在加载...
     */
    public static String REFRESH_HEADER_LOADING = "正在加载...";
    /**
     * 释放立即刷新
     */
    public static String REFRESH_HEADER_RELEASE = "释放立即刷新";
    /**
     * 刷新完成
     */
    public static String REFRESH_HEADER_FINISH = "刷新成功";
    /**
     * 刷新失败
     */
    public static String REFRESH_HEADER_FAILED = "刷新失败";

    private TextView mTitleText;
    private ImageView mLoadIv;

    public LoadingHeaderView(Context context) {
        this(context, null);
    }

    public LoadingHeaderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.view_loading_header, this);
        mTitleText = view.findViewById(R.id.view_load_tv);
        mLoadIv = view.findViewById(R.id.view_load_iv);
        Glide.with(this)
                .asGif()
                .load(R.mipmap.ic_launcher)
                .into(mLoadIv);

    }

    @Override
    public int onFinish(@NonNull RefreshLayout layout, boolean success) {
        if (success) {
            mTitleText.setText(REFRESH_HEADER_FINISH);
        } else {
            mTitleText.setText(REFRESH_HEADER_FAILED);
        }
        super.onFinish(layout, success);
        //延迟500毫秒之后再弹回
        return 500;
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        switch (newState) {
            //下拉过程
            case PullDownToRefresh:
                mTitleText.setText(REFRESH_HEADER_PULLING);
                break;
            //松开刷新
            case ReleaseToRefresh:
                mTitleText.setText(REFRESH_HEADER_RELEASE);
                break;
            //loading中
            case Refreshing:
                mTitleText.setText(REFRESH_HEADER_LOADING);
                break;
            default:
                break;
        }
    }
}
